package br.ufrj.dcc.comp2.musica.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import br.ufrj.dcc.comp2.musica.controle.Cadastrador;
import br.ufrj.dcc.comp2.musica.controle.Musica;
import br.ufrj.dcc.comp2.musica.modelo.Disciplina;
import br.ufrj.dcc.comp2.musica.vista.PainelCadastrarDisciplinas;

public class ListenerCadastrarDisciplinas implements ActionListener {

	private PainelCadastrarDisciplinas painelDisciplinas;
	private String nomeDisciplina;
	private String codigoDisciplina;
	private Short creditosDisciplina;

	public ListenerCadastrarDisciplinas(
			PainelCadastrarDisciplinas painelDisciplinas) {
		this.painelDisciplinas = painelDisciplinas;
	}

	
	public void actionPerformed(ActionEvent evento) {
		nomeDisciplina = painelDisciplinas.getCampoNomeDisciplina().getText();
		codigoDisciplina = painelDisciplinas.getCampoCodigoDisciplina()
				.getText();
		try {
			creditosDisciplina = Short.parseShort(painelDisciplinas
					.getCampoCreditos().getText());
		} catch (Exception e) {
			creditosDisciplina = 0;
		}

		if (creditosDisciplina == 0 || nomeDisciplina.equals("") || codigoDisciplina.equals("")) {
			if (creditosDisciplina == 0) {
				JOptionPane.showMessageDialog(null, "Creditos iguais a zero.",
						" >>>Erro", JOptionPane.ERROR_MESSAGE);
			}
			if (nomeDisciplina.equals("") || codigoDisciplina.equals("")) {
				JOptionPane.showMessageDialog(null, "Campo em branco.",
						" >>>Erro", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			Musica.getInstancia().getCadastrador().cadastrar(new Disciplina(nomeDisciplina, codigoDisciplina, creditosDisciplina));
		}
	}

}
